package net.xdclass.online_class;

import net.xdclass.online_class.dao.VideoMapper;
import net.xdclass.online_class.dao.VideoOrderMapper;
import net.xdclass.online_class.domain.User;
import net.xdclass.online_class.domain.Video;
import net.xdclass.online_class.domain.VideoOrder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class SqlSessionDemo {

    public static void main(String[] args) throws IOException {
        // 读取xml配置文件
        String source = "config/mybatis-config.xml";
        // 构建session工厂
        InputStream is = Resources.getResourceAsStream(source);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        // 获取session

        try(SqlSession sqlSession = sqlSessionFactory.openSession()) {
            VideoMapper videomapper = sqlSession.getMapper(VideoMapper.class);
            VideoOrderMapper videoOrderMapper = sqlSession.getMapper(VideoOrderMapper.class);
//            Video video = videomapper.selectById(44);
//            System.out.println(video.toString());
//
//            List<Video> videoList = videomapper.selectList();
//            for (Video video1 : videoList) {
//                System.out.println(video1);
//            }

//            List<Video> videoList = videomapper.selectByPointAndTitleLike(9.10, "项目");
//            for (Video video2 : videoList) {
//                System.out.println(video2);
//
//            }


//            Video video1 = new Video();
//            video1.setTitle("java哈哈高级教程");
//            video1.setSummary("https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/video/2019_backend/jvm_detail.jpeg");
//            video1.setCoverImg("https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/video/2019_backend/jvm.jpeg");
//            video1.setPrice(9900);
//            video1.setCreateTime(new Date());
//            video1.setPoint(9.20);
//            int rows = videomapper.add(video1);
//            System.out.println(rows);
//            System.out.println(video1.toString());


//            Video video1 = new Video();
//            video1.setTitle("java哈哈高级教程11");
//            video1.setSummary("https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/video/2019_backend/jvm_detail.jpeg");
//            video1.setCoverImg("https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/video/2019_backend/jvm.jpeg");
//            video1.setPrice(9911);
//            video1.setCreateTime(new Date());
//            video1.setPoint(9.11);
//
//            Video video2 = new Video();
//            video2.setTitle("java哈哈高级教程22");
//            video2.setSummary("https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/video/2019_backend/jvm_detail.jpeg");
//            video2.setCoverImg("https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/video/2019_backend/jvm.jpeg");
//            video2.setPrice(9922);
//            video2.setCreateTime(new Date());
//            video2.setPoint(9.22);
//
//            List<Video> list = new ArrayList<>();
//            list.add(video1);
//            list.add(video2);
//            int rows = videomapper.addBatch(list);
//            System.out.println(rows);


//            Video video3 = new Video();
//            video3.setTitle("java哈哈高级教程33");
//            video3.setSummary("33https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/video/2019_backend/jvm_detail.jpeg");
//            video3.setId(51);
//            int rows = videomapper.updateVideo(video3);
//            System.out.println(rows);


//            Video video4 = new Video();
//            video4.setTitle("java哈哈高级教程66");
//            video4.setSummary("44https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/video/2019_backend/jvm_detail.jpeg");
//            video4.setId(50);
//            int rows = videomapper.updateVideoSelective(video4);
//            System.out.println(rows);

//            Map<String,Object> map = new HashMap<>();
//            map.put("createTime","2021-5-6 00:00:00");
//            map.put("price","9000");
//            int rows = videomapper.deleteVideoByCreateTimeAndPrice(map);
//            System.out.println(rows);

//            Video video = videomapper.selectBaseFieldByIdWithResultMap(40);
//            System.out.println(video.toString());


//            List<VideoOrder> videoOrdersList = videoOrderMapper.queryVideoOrderList();
//            System.out.println(videoOrdersList.toString());


//            List<User> userList = videoOrderMapper.queryUserOrderList();
//            System.out.println(userList.toString());
            List<VideoOrder> videoOrderList = videoOrderMapper.queryVideoOrderListLazy();
            System.out.println(videoOrderList.size());
            for (VideoOrder videoOrder : videoOrderList) {
                System.out.println(videoOrder.getUser().getName());
            }


        }


    }
}
